const { list, totalTag, userList, searchList } = require("./mock");
const bodyParser = require("body-parser");
module.exports = app => {
  app.use(bodyParser.json());
  app.get("/api/list", (_, res) => res.send({ code: 200, data: list }));
  app.get("/api/tag/list", (_, res) =>
    res.send({ code: 200, data: totalTag, message: "数据获取成功!!!" })
  );
  app.post("/api/login", (req, res) => {
    const { username, password } = req.body;
    const item = userList.find(
      v => v.username === username && v.password === password
    );
    if (item)
      res.send({
        code: 200,
        message: "登录成功",
        data: {
          id: item.id,
          nickname: item.nickname,
          avator: item.avator,
          list: item.list,
          token: "XXXXXWLMDWDMQPWDOQWDQ" + Math.random(),
        },
      });
    else
      res.send({
        code: 403,
        message: "账户密码错误!!!",
        data: null,
      });
  });
  app.get("/api/search/list", (req, res) => {
    const { value } = req.query;
    if (value)
      res.send({
        code: 200,
        data: searchList.filter(v => {
          return (
            v.id.includes(value) ||
            v.title.includes(value) ||
            v.description.includes(value)
          );
        }),
        message: "OK",
      });
    else res.send({ code: 200, data: searchList, message: "OK" });
  });
};
